package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.query.StratetyCatalogQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalogMapper.insert(strategyCatalog);
    }

    @Override
    public PageInfo query(StratetyCatalogQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyCatalog> list = strategyCatalogMapper.queryForList(qo);
        return new PageInfo(list);
    }


    @Override
    public void delete(Long id) {
        strategyCatalogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        // 默认的序号
        if (strategyCatalog.getSequence() == null) {
                Strategy strategy = strategyCatalog.getStrategy();
                if (strategy != null) {
                    strategyCatalog.setSequence(strategyCatalogMapper.getMaxSquence(strategy.getId()) + 1);
                }
        }
        if (strategyCatalog.getId() != null) {
            strategyCatalogMapper.updateByPrimaryKey(strategyCatalog);
        }else {
            strategyCatalogMapper.insert(strategyCatalog);
        }
    }

    @Override
    public List<StrategyCatalog> getCatalogByStrategyId(Long strategyId) {
        return strategyCatalogMapper.selectCatalogByStrategyId(strategyId);
    }

    @Override
    public List<StrategyCatalog> selectAll() {
        return strategyCatalogMapper.selectAll();
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogMapper.updateByPrimaryKey(strategyCatalog);
    }

    @Override
    public StrategyCatalog get(Long id) {
        return strategyCatalogMapper.selectByPrimaryKey(id);
    }

}
